package com.ws.haungjia.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.haungjia.Bean.*;
import com.ws.haungjia.Service.FenlieService;
import com.ws.haungjia.Service.LvxingService;
import com.ws.haungjia.Service.ShangjiaService;
import com.ws.haungjia.utils.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/luxing")
public class LuxingController {
    @Autowired
    private LvxingService lvyou;
    @Autowired
    FenlieService fenlieService;
    @Autowired
    ShangjiaService shangjiaService;
    @RequestMapping("/chaxun")
    public String chaxun(Model model){
        Huixian2 huixian2 = new Huixian2();
        PageHelper.startPage(huixian2.getYe(),huixian2.getGeshu());
        List<luxian> luxians = lvyou.chaxun();
        PageInfo pageInfo = new PageInfo(luxians);
        List<Fenlie> fenlie = fenlieService.chaxun();
        List<Shangjia> cha = shangjiaService.cha();
        model.addAttribute("shangj",cha);
        model.addAttribute("fenlie",fenlie);
        model.addAttribute("pageInfo",pageInfo);
        return "WEB-INF/houtai/luxianguanli";
    }

    @RequestMapping("/zengjia")
    public String zengjia(luxian luxian, MultipartFile tupian) throws IOException {
        System.out.println(luxian.getZhutu());
        if (tupian.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + tupian.getOriginalFilename().split("\\.")[1];
            luxian.setZhutu(fileName);
//            lastIndexo
            tupian.transferTo(new File(Res.fileDir + fileName));
        }else{
            luxian.setZhutu("");
        }
        int add = lvyou.add(luxian);
        if (add > 0) {
            return "redirect:/luxing/sousuo";
        }else{
            return "WEB-INF/houtai/addluxian";
        }
    }

    @RequestMapping("/shan")
    public String shan(int id){
        int shan = lvyou.shan(id);
        if (shan > 0) {
            return "redirect:/luxing/sousuo";
        }else {
            return "WEB-INF/houtai/xiugailuxian";
        }
    }

    @RequestMapping("/huixian")
    public String huixian(int id,Model model){
        luxian huixian = lvyou.huixian(id);
        model.addAttribute("route",huixian);
        List<Fenlie> luxians = lvyou.chafenlie();
        model.addAttribute("fenlieList",luxians);
        List<Shangjia> chashangjia = lvyou.chashangjia();
        model.addAttribute("shangjiaList",chashangjia);
        return "WEB-INF/houtai/xiugailuxian";
    }

    @RequestMapping("/xiugai")
    public String xiugai(luxian luxian, MultipartFile tupian) throws IOException {
        if (tupian.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + tupian.getOriginalFilename().split("\\.")[1];
            luxian.setZhutu(fileName);
            tupian.transferTo(new File(Res.fileDir + fileName));
        }else {
            luxian.setZhutu("");
        }
        int xiugai = lvyou.xiugai(luxian);
        if (xiugai > 0) {
            return "redirect:/luxing/sousuo";
        }else {
            return "WEB-INF/houtai/xiugailuxian";
        }

    }

    @RequestMapping("/sousuo")
    public String sousuo(Luxianhuixian luxianhuixian, Model model){
        PageHelper.startPage(luxianhuixian.getYe(),luxianhuixian.getGeshu());
        List<luxian> luxians = lvyou.sousuo(luxianhuixian);
        PageInfo pageInfo = new PageInfo(luxians);
        List<Fenlie> fenlie = lvyou.chafenlie();
        List<Shangjia> cha = shangjiaService.cha();
        model.addAttribute("shangj",cha);
        model.addAttribute("fenlie",fenlie);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("luxianhuixian",luxianhuixian);
        return "WEB-INF/houtai/luxianguanli";
    }




    @RequestMapping("/chafenshang")
    public String chafenshang(Model model){
        List<Fenlie> luxians = lvyou.chafenlie();
        model.addAttribute("luxians",luxians);
        List<Shangjia> chashangjia = lvyou.chashangjia();
        model.addAttribute("chashangjia",chashangjia);
        return "WEB-INF/houtai/addluxian";
    }
}